package com.ms.teapuesto.util.xsd;

import com.sun.xml.xsom.*;
import com.sun.xml.xsom.impl.ForeignAttributesImpl;
import com.sun.xml.xsom.parser.XSOMParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * User: JVergara
 * Date: 12/06/13
 */
public class XsdReader {
    private final Logger LOG = LoggerFactory.getLogger(XsdReader.class);

    public static String RETRIEVE_PROPERTIES_MODE = "properties";

    public static String RETRIEVE_LENGTH_MODE = "length";

    public List<String> parseSchema(File file, String seccion, String mode) {
        List<String> result = new LinkedList<String>();
        try {
            XSOMParser parser = new XSOMParser();
            parser.parse(file);
            XSSchemaSet sset = parser.getResult();
            XSSchema mys = sset.getSchema(1);
            Iterator itr = sset.iterateSchema();
            while (itr.hasNext()) {
                XSSchema s = (XSSchema) itr.next();
                LOG.info("¨Parsing Target namespace: " + s.getTargetNamespace());
                XSComplexType ct = mys.getComplexType(seccion);

                XSContentType xsContentType = ct.getContentType();
                XSParticle particle = xsContentType.asParticle();
                if(particle != null){
                    XSTerm term = particle.getTerm();
                    if(term.isModelGroup()){
                        XSModelGroup xsModelGroup = term.asModelGroup();
                        XSParticle[] particles = xsModelGroup.getChildren();
                        for(XSParticle p : particles ){
                            XSTerm pterm = p.getTerm();
                            List attrs = pterm.getForeignAttributes();
                            ForeignAttributesImpl fai = (ForeignAttributesImpl) attrs.get(0);
                            String propertyName = null, propertyLength=null;
                            if (mode.equals(RETRIEVE_PROPERTIES_MODE)) {
                                propertyName = ((XSElementDecl) pterm).getName();
                                propertyLength = fai.getValue(fai.getIndex("jrb:length"));
                                if (propertyName != null && propertyLength != null) {
                                    result.add(propertyName);
                                }
                            } else {
                                propertyLength = fai.getValue(fai.getIndex("jrb:length"));
                                if (propertyLength != null) {
                                    result.add(propertyLength);
                                }
                            }
                            LOG.debug("Procesando: " + propertyName + " jrb:length="+ propertyLength);
                        }
                        if (result.size() > 0) {
                            break;
                        }
                    }
                }


            }
        } catch (Exception exp) {
            exp.printStackTrace(System.out);
        }
        return result;
    }

    public static void main(String args[]) throws IOException {
        XsdReader rr = new XsdReader();
        ClassPathResource resource = new ClassPathResource("xsd/invoice.def.xsd");
        List<String> documento = rr.parseSchema(resource.getFile(), "Documento", XsdReader.RETRIEVE_PROPERTIES_MODE);
        for (String value : documento) {
            System.out.println("Value: " + value);
        }
    }

}
